package controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modelo.Empresa;


/**
 * Servlet implementation class ControllerManterEmpresa
 */
public class ControllerManterEmpresa extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ControllerManterEmpresa() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String operacao = request.getParameter("operacao");
		
		try{
			if(operacao.equals("cadastrarEmpresa")){
				
				Empresa empresa = new Empresa();
					empresa.setNmNomeEmpresa(request.getParameter("nomeEmpresa"));
					empresa.setNmCNPJ(new Integer(request.getParameter("cnpj")));
					empresa.setNmEndereco(request.getParameter("endereco"));
					empresa.setNmNumero(request.getParameter("endereco-numero"));
					empresa.setNmComplemento(request.getParameter("endereco-complemento"));
					empresa.setNmEmail(request.getParameter("email"));
					empresa.setNmTelefone(new Integer(request.getParameter("telefone")));
					
					empresa.cadastrarEmpresa();
					
					request.getRequestDispatcher("Principal.jsp").forward(request,response);
			
			} else if(operacao.equals("consultarEmpresa")){
				
			}
			
		}catch (Exception e) {
			// CHAMA O METODO DE ERRO
			tratarErro(request, response, e);
		} // FIM TRY
	}
	
	private void tratarErro(HttpServletRequest request, HttpServletResponse response, Exception e) throws ServletException, IOException {
		
		request.getSession().setAttribute("erro", e.toString());
		request.getRequestDispatcher("erro.jsp").forward(request, response);
	} // FIM TRATA ERRO
}
